//
//  ReginViewController.m
//  EasyChat
//
//  Created by dllo on 16/6/14.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ReginViewController.h"
#import "TextFieldBackground.h"


#import "AppDelegate.h"


@interface ReginViewController ()<UITextFieldDelegate>
@property (nonatomic, retain) UITextField *userName;
@property (nonatomic, retain) UITextField *passWord;
@property (nonatomic, retain) UIButton *resgin;
@property(nonatomic,retain)UILabel *TitleLabel;
@property(nonatomic,retain) TextFieldBackground *background;
@property(nonatomic,retain)UIButton *backButton;
//@property (nonatomic, retain) UIButton *login;

@end

@implementation ReginViewController

-(void)dealloc{
    [super dealloc];
    [_userName release];
    [_passWord release];
    [_resgin release];
    [_TitleLabel release];
    [_background release];
    [_backButton release];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor colorWithRed:51/255.0 green:204/255.0 blue:255/255.0 alpha:1]];
    //标题label
    self.TitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 90, self.view.frame.size.width, 50)];
    self.TitleLabel.backgroundColor = [UIColor clearColor];
    self.TitleLabel.text = @"EasyChat";
    self.TitleLabel.textColor = [UIColor whiteColor];
    self.TitleLabel.font = [UIFont systemFontOfSize:30];
    self.TitleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:self.TitleLabel];
    
    //封装的view
    _background=[[TextFieldBackground alloc] initWithFrame:CGRectMake(20, 160, self.view.frame.size.width-40, 100)];
    [_background setBackgroundColor:[UIColor whiteColor]];
    [[_background layer] setCornerRadius:5];
    [[_background layer] setMasksToBounds:YES];
    [self.background drawRect:CGRectMake(40, 210, self.view.frame.size.width - 80, 1)];
    [self.view addSubview:_background];
    
    
    // 创建用户UITextField
    self.userName = [[UITextField alloc] initWithFrame:CGRectMake(10, 0, self.view.frame.size.width-40, 50)];
    self.userName.backgroundColor = [UIColor clearColor];
    self.userName.layer.cornerRadius= 10;
    
    self.userName.placeholder=[NSString stringWithFormat:@"请输入您的账号"];
    [self.background addSubview:self.userName];
    self.userName.delegate = self;
    self.userName.delegate = self;
    // 创建密码UITextField
    self.passWord = [[UITextField alloc] initWithFrame:CGRectMake(10, 51, self.view.frame.size.width - 40, 49)];
    self.passWord.backgroundColor = [UIColor clearColor];
    self.passWord.layer.cornerRadius= 10;
    self.passWord.placeholder=[NSString stringWithFormat:@"请输入您的密码"];
    [self.background addSubview:self.passWord];
    
    self.passWord.delegate = self;
    self.passWord.delegate = self;
      [self.passWord addTarget:self  action:@selector(valueChanged)  forControlEvents:UIControlEventEditingChanged];
    
    // 注册按钮
   
    self.resgin = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.resgin setFrame:CGRectMake(20, 270, self.view.frame.size.width-40, 50)];
    
    [self.resgin setTitle:@"马上注册" forState:UIControlStateNormal];
    [self.resgin setBackgroundColor:[UIColor colorWithRed:51/255.0 green:102/255.0 blue:255/255.0 alpha:1]];
    self.resgin.layer.cornerRadius = 10;
    self.resgin.titleLabel.font = [UIFont systemFontOfSize:22];
    [self.resgin setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [self.resgin addTarget:self action:@selector(resginAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.resgin];
    
    //返回按钮
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.backButton.frame = CGRectMake(self.view.frame.size.width - 100,350,80,30);
    [self.backButton addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.backButton setTitle:@"返回" forState:UIControlStateNormal];
    [self.backButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.backButton.backgroundColor = [UIColor clearColor];
    
    [self.view addSubview:self.backButton];
    
    
    
}

//监听密码textfield  当输入内容后 登录按钮字体颜色变成白色
-(void)valueChanged{
    
    [self.resgin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
}

//返回按钮的点击方法
-(void)backButtonAction{

[self dismissViewControllerAnimated:YES completion:^{
    
    
}];

}



// 注册按钮的点击方法
- (void)resginAction:(UIButton *)button
{
    [[EaseMob sharedInstance].chatManager asyncRegisterNewAccount:self.userName.text password:self.passWord.text withCompletion:^(NSString *username, NSString *password, EMError *error) {
        if (!error) {
            NSLog(@"注册成功");
            self.passValue(self.userName.text, self.passWord.text);
            [self dismissViewControllerAnimated:YES completion:^{
                //通知 登录页面 按钮颜色改变
                [[NSNotificationCenter defaultCenter] postNotificationName:UITextFieldTextDidChangeNotification object:nil];
            }];
            
        }else
        {
            NSLog(@"注册失败 error === %@", error);
        }
        
    } onQueue:dispatch_get_main_queue()];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    return YES;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
